package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.ThrowInfo;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.core.room.DoorController;
import gatperdut.chato.core.room.RoomController;

public class ThrowAction extends Action {
    public ThrowAction( ThrowInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        if ( emptyEmote() ) {
            getEntityController().tx( "Empty emotes are not allowed." );
            return CommandExecutionResult.OK;
        }

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        if ( !checkUnindexedWord( getInfo().getWhat() ) ) {
            return CommandExecutionResult.OK;
        }
        ItemController ic = getEntityController().getInventoryController().getHeldItemController( getInfo().getWhat() );
        if ( ic == null ) {
            getEntityController().tx( "You don't seem to be holding that." );
            return CommandExecutionResult.OK;
        }

        RoomController rc = getEntityController().getRoomController().getExitsController().getAdjacentRoomController( getInfo().getDirection() );
        if ( rc == null ) {
            getEntityController().tx( "You cannot throw anything in that direction." );
            return CommandExecutionResult.OK;
        }

        DoorController dc = getEntityController().getRoomController().getExitsController().getAdjacentDoorController( getInfo().getDirection() );
        if ( dc != null ) {
            if ( !dc.getDoor().isOpen() ) {
                getEntityController().tx( dc.getDoor().getDesc() + " is closed." );
                return CommandExecutionResult.OK;
            }
        }

        getEntityController().getInventoryController().takeFromInventory( ic );

        rc.getInventoryController().putInInventory( ic );

        getEntityController().getEchoes().txThrow( ic, getInfo().getDirection(), rc, emote );

        return CommandExecutionResult.OK;
    }

    @Override
    public ThrowInfo getInfo() {
        return (ThrowInfo) super.getInfo();
    }

    @Override
    public boolean interruptsCrafting() {
        return false;
    }

}
